/**
 * Smart City use case October 2012 
 */
package com.orange.smartcity.android;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RemoteViews;

import com.orange.smartcity.android.activity.Actions;
import com.orange.smartcity.android.activity.EventList;
import com.orange.smartcity.android.activity.Home;
import com.orange.smartcity.android.activity.Settings;

/**
 * @author FAIROUZ OUBENALI
 * 
 */
public class SmartCityCommon {
	// this class can not be initialized
	private SmartCityCommon() {

	}

	/*
	 * ==========================================================================
	 * = * TAG name of classes used on log messages * *
	 * ======================================================================
	 */

	/**
	 * package com.orange.smartcity.android
	 */
	public final static String SMART_CITY_COMMON_TAG = "SmartCityCommon";
	public final static String SMART_CITY_APP_TAG = "SmartCityApplication";
	public final static String CEP_EVENT_RECEIVER_TAG = "CEPEventReceiver";

	/**
	 * package com.orange.smartcity.android.activity
	 */
	public final static String HOME_ACTIVITY_TAG = "Home";
	public final static String SETTING_ACTIVITY_TAG = "Settings";
	public final static String ACTION_ACTIVITY_TAG = "Actions";
	public final static String EVENT_LIST_ACTIVITY_TAG = "EventList";

	/**
	 * package com.orange.smartcity.android.db
	 */
	public final static String SMART_CITY_DATA_BASE_TAG = "SmartCityDataBase";
	public final static String EVENT_DATA_BASE_TAG = "Event";

	/**
	 * package com.orange.smartcity.android.tools
	 */
	public final static String PREFERENCE_UTILS_TAG = "PreferencesUtils";
	public final static String MENU_UTILS_TAG = "ApplicationMenuUtils";
	public final static String GPS_UTILS_TAG = "GpsLocationUtils";
	public final static String IMAGE_LOADER_UTILS_TAG = "ImageLoaderUtils";

	/*
	 * ==========================================================================
	 * = * methods shared * *
	 * ======================================================================
	 */
	
	/**
	 * Issues a notification to inform the user that server has sent an event.
	 * 
	 * @param context
	 * @param message
	 *            event context
	 * @param cloudTypeName
	 *            type of events cloud
	 * @param imgName
	 *            icon
	 */
	public static void notification(Context context, String message,
			String cloudTypeName, int imgName) {
		Log.i(SMART_CITY_COMMON_TAG, "\n notification method");
		int icon = imgName;
		if(cloudTypeName.equals("TRANSPORT"))
		 icon = R.drawable.transport;
		else 
			icon = R.drawable.shopping;
		long when = System.currentTimeMillis();
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(icon, message, when);
		String title = cloudTypeName;
		Intent notificationIntent = new Intent(context, Home.class);
		// set intent so it does not start a new activity
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent intent = PendingIntent.getActivity(context, 0,
				notificationIntent, 0);
		notification.setLatestEventInfo(context, title, message, intent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		notification.sound = Uri.withAppendedPath(
				Audio.Media.INTERNAL_CONTENT_URI, "20");
		if(cloudTypeName.equals("TRANSPORT"))
		notificationManager.notify(0, notification);
		else if(cloudTypeName.equals("SHOPPING"))
			notificationManager.notify(1, notification);
			else if(cloudTypeName.equals("MARKET"))
				notificationManager.notify(2, notification);
	}


	/**
	 * test Issues a notification to inform the user that server has sent a
	 * message.
	 * 
	 * @param context
	 * @param message
	 */
	@SuppressWarnings("deprecation")
	public static void notificationEvent(Context context, String message,
			String cloudTypeName, int imgName) {
		Log.i(SMART_CITY_COMMON_TAG, "notification event");
		final int CUSTOM_VIEW_ID = 1;
		int icon=0;
		if(cloudTypeName.equals("TRANSPORT"))
		 icon = R.drawable.transport;
		else 
			icon = R.drawable.shopping;
		long when = System.currentTimeMillis();
		Notification notification = new Notification(icon, cloudTypeName, when);
		// flags
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		notification.flags |= Notification.FLAG_ONGOING_EVENT;
		 notification.sound = Uri.withAppendedPath(
		 Audio.Media.INTERNAL_CONTENT_URI, "20");
		RemoteViews contentView = new RemoteViews(context.getPackageName(),
				R.layout.custom_notification);
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		contentView.setImageViewResource(R.id.image, R.drawable.shopping);
		contentView.setTextViewText(R.id.title, cloudTypeName);
		contentView.setTextViewText(R.id.text, message);
		
		notification.contentView = contentView;
		Intent notificationIntent = new Intent(context, Home.class);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
				notificationIntent, 0);
		notification.contentIntent = contentIntent;
		notificationManager.notify(CUSTOM_VIEW_ID, notification);

	}

	/*
	 * activity setting
	 */
	public static void getSettingsPref(Context context) {
		Intent preferences = new Intent(context, Settings.class);
		preferences.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		context.startActivity(preferences);
	}

	/*
	 * activity setting
	 */
	public static void getHome(Context context) {
		Intent home = new Intent(context, Home.class);
		home.setAction(Intent.ACTION_VIEW);
		home.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		context.startActivity(home);
	}

	/*
	 * activity actions
	 */
	public static void getActions(Context context) {
		Intent actions = new Intent(context, Actions.class);
		actions.setAction(Intent.ACTION_VIEW);
		actions.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		context.startActivity(actions);
		
	}

	/*
	 * activity actions
	 */
	public static void getEventList(Context context) {
		Intent eventList = new Intent(context, EventList.class);
		eventList.setAction(Intent.ACTION_VIEW);
		eventList.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		context.startActivity(eventList);
	}

	// * = * footer bar * *

	public static void setFooterHomeBackground() {
		mLayoutFooterSettings.setBackgroundResource(R.color.transparent);
		mLayoutFooterEvents.setBackgroundResource(R.color.holo_orange_light);
		mLayoutFooterActions.setBackgroundResource(R.color.holo_orange_light);

	}

	public static void setFooterActionBackground() {
		// footer background color
		mLayoutFooterActions.setBackgroundResource(R.color.transparent);
		mLayoutFooterSettings.setBackgroundResource(R.color.holo_orange_light);
		mLayoutFooterEvents.setBackgroundResource(R.color.holo_orange_light);
	}

	public static void setFooterEventBackground() {
		// background color
		mLayoutFooterEvents.setBackgroundResource(R.color.transparent);
		mLayoutFooterSettings.setBackgroundResource(R.color.holo_orange_light);
		mLayoutFooterActions.setBackgroundResource(R.color.holo_orange_light);
	}

	/*
	 * ==========================================================================
	 * = * Attributes shared * *
	 * ======================================================================
	 */
	
	// * = * footer bar * *
	public static ImageView mFooterSetting;
	public static ImageView mFooterActions;
	public static ImageView mFooterEventList;
	public static String currentActivity = ACTION_ACTIVITY_TAG;
	public static LinearLayout mLayoutFooterSettings;
	public static LinearLayout mLayoutFooterActions;
	public static LinearLayout mLayoutFooterEvents;
	public static boolean isFirstImage = true;
	public static boolean isMeterChecked = true;
	public static boolean isOnline = false;

	/*
	 * ==========================================================================
	 * = * enumeration type * *
	 * ======================================================================
	 */
	public static enum KINDS {
		PROFILE, PREFERENCES,CHECK, NOTIFICATION, DISTANCE,SAVE, ACTIONS, SETTINGS, EVENT_LIST, SEARCH, EVENT_SEARCH, LIKE, UNLIKE, MAPS, IMAGE, MUSICS, MAP_NAVIGATION, MAP_ADDRESS
	};

	/**
	 * The link type
	 */

	public static enum Type {
		FILE, IMAGE, VIDEO, AUDIO, URL, UNKNOWN
	}

	/**
	 * The theme of event
	 */

	public static enum CloudTypeEvent {
		EDUCATION, HEALTH, SPORTS, SHOPPING, TRANSPORT, RESTAURANT, LEISURE, CULTURE, OTHER, TEST
	}

	/**
	 * type of event source schema
	 */
	public static enum EventSourceType {
		XML, XSD, JSON, MAP, TEXT_SCHEMA
	}

	public static enum ProfileStatus {
		online, offline
	}

}
